method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: text, history: chatHistory.slice(-10) }) }).then(function(r) { return r.json(); }).then(function(data) { var reply = (data && data.reply) ? data.reply : (data && data.error ? data.error : '无法获取回复'); addChatMsg('assistant', reply); chatHistory.push({ role: 'assistant', content: reply }); }).catch(function() { addChatMsg('assistant', '网络错误,请稍后再试。'); }).finally(function() { if (sendBtn) { sendBtn.disabled = false; sendBtn.textContent = '发送'; } }); } if (chatSend) chatSend.addEventListener('click', sendChat); if (chatInput) chatInput.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendChat(); } }); function runSearch() { var inp = document.getElementById('searchNewsInput'); var kw = (inp && inp.value !== undefined) ? String(inp.value).trim() : ''; if (!searchResults) return; searchResults.innerHTML = ''; if (!kw) { searchResults.innerHTML = '

请输入关键词后点击搜索

'; return; } searchResults.innerHTML = '

正在用 AI 查找相关新闻…

'; var searchUrl = newsSearchApiUrl + '?q=' + encodeURIComponent(kw); fetch(searchUrl).then(function(r) { return r.json().then(function(data) { return { ok: r.ok, data: data }; }); }).then(function(res) { var data = res.data || {}; var list = (data.list && Array.isArray(data.list)) ? data.list : []; searchResults.innerHTML = ''; if (list.length === 0) { searchResults.innerHTML = '

' + (data.error || '未找到相关新闻') + '

'; return; } list.forEach(function(item) { var a = document.createElement('a'); a.href = item.url || '#'; a.textContent = item.title || ''; if (item.url) { a.target = '_blank'; a.rel = 'noopener'; } searchResults.appendChild(a); }); }).catch(function() { searchResults.innerHTML = '

网络异常,请检查连接后重试

'; }); } if (searchBtn) searchBtn.addEventListener('click', runSearch); if (searchInput) searchInput.addEventListener('keydown', function(e) { if (e.key === 'Enter') { e.preventDefault(); runSearch(); } }); })(); (function() { function scrollToTop() { window.scrollTo({ top: 0, behavior: 'smooth' }); } function scrollToBottom() { window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' }); } var topBtn = document.getElementById('scrollTopBtn'); var bottomBtn = document.getElementById('scrollBottomBtn'); if (topBtn) topBtn.addEventListener('click', scrollToTop); if (bottomBtn) bottomBtn.addEventListener('click', scrollToBottom); })();